/*
	The hello world demo
*/
#include "osapi.h"
#include "user_interface.h"
#include "gpio.h"
#include "driver/uart.h"
#include "esp_simple_pair.h"
#include "esp_discovery.h"

/*------------------------------------------------------------------------------------------------*/
// see eagle_soc.h for these definitions
#define LED_GPIO 		2
#define LED_GPIO_MUX 	PERIPHS_IO_MUX_GPIO2_U
#define LED_GPIO_FUNC 	FUNC_GPIO2

/*------------------------------------------------------------------------------------------------*/
void contiki_start(void);

/*------------------------------------------------------------------------------------------------*/
void ICACHE_FLASH_ATTR led_set_state(int led_state)
{
	GPIO_OUTPUT_SET(LED_GPIO, led_state);
}

/******************************************************************************
 * FunctionName : user_rf_cal_sector_set
 * Description  : SDK just reversed 4 sectors, used for rf init data and paramters.
 *                We add this function to force users to set rf cal sector, since
 *                we don't know which sector is free in user's application.
 *                sector map for last several sectors : ABBBCDDD
 *                A : rf cal
 *                B : at parameters
 *                C : rf init data
 *                D : sdk parameters
 * Parameters   : none
 * Returns      : rf cal sector
*******************************************************************************/
uint32 ICACHE_FLASH_ATTR user_rf_cal_sector_set(void)
{
    enum flash_size_map size_map = system_get_flash_size_map();
    uint32 rf_cal_sec = 0;

    switch (size_map) {
        case FLASH_SIZE_4M_MAP_256_256:
            rf_cal_sec = 128 - 8;
            break;

        case FLASH_SIZE_8M_MAP_512_512:
            rf_cal_sec = 256 - 5;
            break;

        case FLASH_SIZE_16M_MAP_512_512:
        case FLASH_SIZE_16M_MAP_1024_1024:
            rf_cal_sec = 512 - 5;
            break;

        case FLASH_SIZE_32M_MAP_512_512:
        case FLASH_SIZE_32M_MAP_1024_1024:
            rf_cal_sec = 1024 - 5;
            break;

        default:
            rf_cal_sec = 0;
            break;
    }

    return rf_cal_sec;
}

void ICACHE_FLASH_ATTR user_rf_pre_init(void)
{
}

void ICACHE_FLASH_ATTR wifi_handle_event_cb(System_Event_t *evt)
{
//	os_printf("wifi event %x\n", evt->event);
	switch(evt->event){
	case EVENT_STAMODE_CONNECTED:
		os_printf("connect to ssid %s, channel %d\n",evt->event_info.connected.ssid, evt->event_info.connected.channel);
		break;

	case EVENT_STAMODE_DISCONNECTED:
		os_printf("disconnect from ssid %s, reason %d\n", evt->event_info.disconnected.ssid, evt->event_info.disconnected.reason);
		break;

	case EVENT_STAMODE_AUTHMODE_CHANGE:
		os_printf("mode: %d -> %d\n", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode);
		break;

	case EVENT_STAMODE_GOT_IP:
	 	os_printf("ip:"IPSTR", mask:"IPSTR", gw:"IPSTR,
	 			 IP2STR(&evt->event_info.got_ip.ip),
				 IP2STR(&evt->event_info.got_ip.mask),
				 IP2STR(&evt->event_info.got_ip.gw));
	 	os_printf("\n");
		break;

//	case EVENT_STAMODE_DHCP_TIMEOUT:
//		break;

	case EVENT_SOFTAPMODE_STACONNECTED:
		os_printf("station "MACSTR"join, AID = %d\n", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid);
		break;

	case EVENT_SOFTAPMODE_STADISCONNECTED:
		os_printf("station "MACSTR "leave, AID = %d\n", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid);
		break;

//	case EVENT_SOFTAPMODE_PROBEREQRECVED:
//		break;

//	case EVENT_OPMODE_CHANGED:
//		break;

//	case EVENT_SOFTAPMODE_DISTRIBUTE_STA_IP:
//		break;
	}
}

void ICACHE_FLASH_ATTR wifi_config(void)
{
//	char ssid[] = "esp32";
//	char password[] = "2017-2018";
//	struct	station_config	stationConf;
//
//	stationConf.bssid_set = 0;		//need	not	check	MAC	address	of	AP
//	os_memcpy(&stationConf.ssid, ssid,	os_strlen(ssid) + 1);
//	os_memcpy(&stationConf.password, password,	os_strlen(password) + 1);
//
//	wifi_station_set_config(&stationConf);

	uint8_t softap_mac[6];
	struct softap_config softapconfig;

	os_memset(&softapconfig, 0, sizeof(softapconfig));
	wifi_get_macaddr(SOFTAP_IF, softap_mac);
	softapconfig.ssid_len = os_sprintf((char*)softapconfig.ssid, "esp8266-%02x%02x%02x%02x%02x%02x", MAC2STR(softap_mac));
	softapconfig.channel = 12; // RF_CHANNEL
	softapconfig.authmode = AUTH_OPEN;

	wifi_softap_set_config_current(&softapconfig);

	wifi_set_opmode_current(STATIONAP_MODE);
	wifi_set_event_handler_cb(wifi_handle_event_cb);
}

void ICACHE_FLASH_ATTR application_start(void)
{
	os_printf("\nApplication_start wifi opmode: %d, channel: %d\n", wifi_get_opmode(), wifi_get_channel());

	contiki_start();

//	esp_simple_pair_start(STATION_MODE); // STATION_MODE / STATIONAP_MODE
	esp_discovery();
}

void ICACHE_FLASH_ATTR user_init(void)
{
	// Configure the UART
	uart_init(BIT_RATE_74880, BIT_RATE_74880);

	os_printf("\n\napplication start.sdk version: %s\r\n", system_get_sdk_version());

	// Configure pin as a GPIO
	PIN_FUNC_SELECT(LED_GPIO_MUX, LED_GPIO_FUNC);  // LED

	// wifi config
	wifi_config();

	system_init_done_cb(application_start);
}
